const obtn = document.getElementById("btn")
obtn.onclick = function(){
    ajax({
        url:"http://127.0.0.1:8002/categroy/findCategroy?parentId=0",
        success(msg){
            console.log(msg)
        },
        error(msg){
            console.log(msg);
        }
    })
    
}

function ajax({url,method="GET",async=true,data={},success,error}){
    let xmlhttp = new XMLHttpRequest()
    xmlhttp.open(method,url,async)
    xmlhttp.send(data)
    xmlhttp.onreadystatechange = function(){
        //进行状态码判断
        if(xmlhttp.status >= 200 && xmlhttp.readyState == 4){
            const res = xmlhttp.responseText
            success(res)
        }else{
            const errorMsg = xmlhttp.responseText
            error(errorMsg)
        }
    }
}
function sendMessage(){
    //步骤一:
    // 这个对象无需导入,直接创建,浏览器默认提供对象
    // 前端发送异步请求核心对象
    let xmlhttp = new XMLHttpRequest()

    //步骤二:
    //建立客户端和服务器的连接
    xmlhttp.open("GET","http://127.0.0.1:8002/categroy/findCategroy?parentId=0",true)
    //发送请求
    xmlhttp.send()

    //步骤三:
    // 请求成功还是失败,前端需要监听状态码.获取后端响应结果
    // onreadystatechange事件,持续不断监听状态码
    xmlhttp.onreadystatechange = function(){
        //进行状态码判断
        if(xmlhttp.status >= 200 && xmlhttp.readyState == 4){
            const res = xmlhttp.responseText
            console.log("后端返回的数据:",res);
        }
    }
}